package com.ella.entity.security;
import com.alibaba.fastjson2.annotation.JSONField;
import com.ella.entity.SysUser;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 登录用户身份权限
 * 
 * @author ruoyi
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class LoginUser implements UserDetails{

    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 权限列表
     */
    private Set<String> permissions;

    //redis默认不会把这个变量序列化，需要开启注解
     @JSONField(serialize = false)
     public List<SimpleGrantedAuthority> Authorities;

    /**
     * 用户信息
     */
    private SysUser user;


    public LoginUser(SysUser user, Set<String> permissions)
    {
        this.user = user;
        this.permissions = permissions;
    }

    public LoginUser(Long userId, SysUser user, Set<String> permissions)
    {
        this.userId = userId;
        this.user = user;
        this.permissions = permissions;
    }

    @JSONField(serialize = false)
    @Override
    public String getPassword()
    {
        return user.getPassword();
    }

    @Override
    public String getUsername()
    {
        return user.getUserName();
    }

    /**
     * 账户是否未过期,过期无法验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired()
    {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     * 
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked()
    {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     * 
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired()
    {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     * 
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isEnabled()
    {
        return true;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (Authorities != null) {
            return Authorities;
        }
		/* 将权限信息封装到Authorities中
		List<GrantedAuthority> authorityList=new ArrayList<>();
		for (String permission : permissions) {
			//构造函数中的参数是权限信息->role
			SimpleGrantedAuthority simpleGrantedAuthority=new SimpleGrantedAuthority(permission);
			authorityList.add(simpleGrantedAuthority);
		}
		Authorities = permissions.stream().map(item -> {
			SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(item);
			Authorities.add(simpleGrantedAuthority);
			return simpleGrantedAuthority;
		}).collect(Collectors.toList());*/
        Authorities = permissions.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
        return Authorities;
    }

    @Override
    public String toString() {
        return "LoginUser{" +
                "userId=" + userId +
                ", token='" + token + '\'' +
                ", loginTime=" + loginTime +
                ", expireTime=" + expireTime +
                ", ipaddr='" + ipaddr + '\'' +
                ", loginLocation='" + loginLocation + '\'' +
                ", browser='" + browser + '\'' +
                ", os='" + os + '\'' +
                ", permissions=" + permissions +
                ", Authorities=" + Authorities +
                ", user=" + user +
                '}';
    }
}
